package com.xyzh.application.base.controller;

import com.xyzh.application.config.log.LogRecord;
import com.xyzh.application.config.log.OperateEnum;
import com.xyzh.application.base.service.BasicService;
import com.xyzh.application.common.constants.ReqPage;
import com.xyzh.application.common.util.ResultHelper;
import com.xyzh.application.common.validation.Create;
import com.xyzh.application.common.validation.PrimaryKey;
import com.xyzh.application.common.validation.Query;
import com.xyzh.application.common.validation.Update;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BasicController<S extends BasicService, T extends Serializable> {

    @Autowired
    public S s;

    @PostMapping("/queryAll")
    @ApiOperation(value = "列表查询")
    @LogRecord(appName = "安居后台", logType = 2, category = "t", bizNo = "capacityAnalysis",
            content =  "'列表查询操作,当前入参(t):'+#t+',操作结果编码:'+#r.code+',操作结果:'+#r.data",
            model = "系统管理-基础数据", operateEnum = OperateEnum.SELECT)
    public ResultHelper queryAll(@RequestBody T t) {
        return ResultHelper.succeed(s.list(t));
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @LogRecord(appName = "安居后台", logType = 2, category = "t", bizNo = "save",
            content =  "'新增操作,当前入参(t):'+#t+',操作结果编码:'+#r.code+',操作结果:'+#r.messasge",
            model = "系统管理-基础数据", operateEnum = OperateEnum.INSERT)
    public ResultHelper save(@RequestBody @Validated(Create.class) T t) {
        boolean save = s.saveEntity(t);
        if (!save) {
            return ResultHelper.failed2Msg("新增失败");
        }
        return ResultHelper.succeed(t);
    }

    @PostMapping("/checkExist")
    @ApiOperation(value = "唯一性检查")
    public ResultHelper checkUnique(@RequestBody @Validated(Query.class) T t) {
        Map map = new HashMap();
        map.put("exist", s.countEntity(t));
        return ResultHelper.succeed(map);
    }

    @PostMapping("/update")
    @ApiOperation(value = "编辑")
    @LogRecord(appName = "安居后台", logType = 2, category = "t", bizNo = "save",
            content =  "'编辑操作,当前入参(t):'+#t+',操作结果编码:'+#r.code+',操作结果:'+#r.message",
            model = "系统管理-基础数据", operateEnum = OperateEnum.UPDATE)
    public ResultHelper update(@RequestBody @Validated(Update.class) T t) {
        boolean update = s.updateEntity(t);
        if (!update) {
            return ResultHelper.failed2Msg("编辑失败");
        }
        return ResultHelper.succeed(t);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @LogRecord(appName = "安居后台", logType = 2, category = "t", bizNo = "save",
            content =  "'删除操作,当前入参(t):'+#t+',操作结果编码:'+#r.code+',操作结果:'+#r.message",
            model = "系统管理-基础数据", operateEnum = OperateEnum.DELETE)
    public ResultHelper delete(@RequestBody @Validated(PrimaryKey.class) T t) {
        boolean delete = s.deleteEntity(t);
        if (!delete) {
            return ResultHelper.failed2Msg("删除失败");
        }
        return ResultHelper.succeed("删除成功");
    }

    @PostMapping("/deleteObject")
    @ApiOperation(value = "删除要素，可以没有id")
    @LogRecord(appName = "安居后台", logType = 2, category = "t", bizNo = "save",
            content =  "'删除操作,当前入参(t):'+#t+',操作结果编码:'+#r.code+',操作结果:'+#r.message",
            model = "基础数据", operateEnum = OperateEnum.DELETE)
    public ResultHelper deleteObject(@RequestBody @Validated(PrimaryKey.class) T t) {
        boolean delete = s.deleteEntityNotId(t);
        if (!delete) {
            return ResultHelper.failed2Msg("删除失败");
        }
        return ResultHelper.succeed("删除成功");
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除")
    public ResultHelper delete(@RequestBody @Validated(PrimaryKey.class) List<T> list) {
        return ResultHelper.succeed(s.deleteBatchEntity(list));
    }

    /**
     * @Description: 模糊查询+分页+排序（实体要集成PageParam）
     * @Param:
     * @return:
     * @Author: CXB
     * @Date: 2022/3/14 10:12
     **/
    @PostMapping("/queryPageByLike/{pageCurrent}/{pageSize}")
    @ApiOperation(value = "模糊分页查询")
    @LogRecord(appName = "安居后台", logType = 2, category = "t", bizNo = "save",
            content =  "'模糊分页查询操作,当前入参(t):'+#t+',当前页(pageCurrent):'+#pageCurrent+',每页大小(pageSize):'+#pageSize+',操作结果编码:'+#r.code+',操作结果:'+#r.data",
            model = "系统管理-基础数据", operateEnum = OperateEnum.PAGENATION)
    public ResultHelper queryPageByLike(@RequestBody T t, @PathVariable("pageCurrent") Integer pageCurrent, @PathVariable("pageSize") Integer pageSize) {
        ReqPage<T> reqPage = new ReqPage<>();
        reqPage.setPageCurrent(pageCurrent);
        reqPage.setData(t);
        reqPage.setPageSize(pageSize);
        return s.queryLikeByPage(reqPage);
    }

//
    @PostMapping("/pageQuery")
    @ApiOperation(value = "分页查询")
    @LogRecord(appName = "安居后台", logType = 2, category = "reqPage", bizNo = "save",
            content =  "'分页查询操作,当前入参(reqPage):'+#reqPage+',操作结果编码:'+#r.code+',操作结果:'+#r.data",
            model = "系统管理-基础数据", operateEnum = OperateEnum.PAGENATION)
    public ResultHelper pageQuery(@RequestBody ReqPage<T> reqPage) {
        return s.queryByPage(reqPage);
    }

}